﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;

namespace PhoneUI.Controls
{
    public delegate void NewOrderEventHandler(object sender,NewOrderEventArgs e);
    public partial class AddNewOrder : UserControl
    {
        private Popup NewOrderPopup;
        private ChoosePic choosepic;
        //点击确定事件
        public event NewOrderEventHandler OnSure;

        public event RoutedEventHandler OnCancel;

        public bool IsOpen
        {
            get
            {
                if (NewOrderPopup != null)
                    return NewOrderPopup.IsOpen;
                else
                    return false;
            }
            set
            {
                NewOrderPopup.IsOpen = value;
            }
        }

        public AddNewOrder()
        {
            InitializeComponent();
        }

        private void Sure_Click(object sender, RoutedEventArgs e)
        {
            if (ActName.Text.Trim().Length == 0)
            {
                MessageBox.Show("请输入动作名称");
                return;
            }
            if (OnSure != null)
            {
                NewOrderEventArgs ne=new NewOrderEventArgs();
                ne.ImagePath=ShortIcon.Tag.ToString();
                OnSure.Invoke(this, ne);
            }
            NewOrderPopup.IsOpen = false;
        }

        private void Cancel_Click(object sender, RoutedEventArgs e)
        {
            if (OnCancel != null)
            {
                OnCancel(sender, e);
            }
            NewOrderPopup.IsOpen = false;
        }

        public void Show()
        {
            NewOrderPopup = new Popup();
            NewOrderPopup.Closed += new EventHandler(NewOrderPopup_Closed);
            NewOrderPopup.Child = this;
            NewOrderPopup.Opacity = 1;
            NewOrderPopup.Width = 480;
            NewOrderPopup.Height = 200;
            NewOrderPopup.HorizontalOffset = 0;
            NewOrderPopup.VerticalOffset = 25;
            NewOrderPopup.IsOpen = true;
        }

        void NewOrderPopup_Closed(object sender, EventArgs e)
        {
            if (choosepic != null)
            {
                choosepic.Hide();
            }
        }

        private void UserControl_LostFocus(object sender, RoutedEventArgs e)
        {
            //NewOrderPopup.IsOpen = false;
        }

        private void ChooseIcon_Click(object sender, RoutedEventArgs e)
        {
            choosepic = new ChoosePic();
            choosepic.SelectonChanged += new SelectionChangedEventHandler(choosepic_SelectonChanged);
            choosepic.Show();
        }

        void choosepic_SelectonChanged(object sender, SelectionChangedEventArgs e)
        {
            ShortCut sc = e.AddedItems[0] as ShortCut;
            ShortIcon.Source = sc.Image;
            ShortIcon.Tag = sc.ImagePath;
        }
    }

    public class NewOrderEventArgs:EventArgs
    {
        public string ImagePath;
    }
}
